// pages/open-pay/open-pay.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    'order_no' : ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    this.order_no = options.order_no
    wx.login({
      success (res) {
        wx.request({
          url: 'https://openpay.duolaibei.com/api/callbackwx',
          data:{
            code:res.code,
            order_no:options.order_no
          },
          method:'POST',
          success(res){
            if(res.data.code == 200){
              wx.requestPayment({
                nonceStr: res.data.content.nonceStr,
                package: res.data.content.package,
                paySign: res.data.content.paySign,
                signType: res.data.content.signType,
                timeStamp: res.data.content.timeStamp,
                success(res){
                  wx.reLaunch({
                    url: '/pages/wview/web?status=1&order_no='+that.order_no
                  })
                },
                fail(err){
                  if(res.errMsg == 'requestPayment:fail cancel'){ //支付取消
                    wx.reLaunch({
                      url: '/pages/wview/web?status=2&order_no='+that.order_no
                    })
                 }else{
                    wx.reLaunch({
                      url: '/pages/wview/web?status=3&order_no='+that.order_no
                    })
                 }
                }
              })
            }else{
              wx.showModal({
                title: '提示',
                content: res.data.msg,
                success (res) {
                  if (res.confirm) { //确定
                    wx.reLaunch({
                      url: '/pages/wview/web?status=4&order_no='+that.order_no
                    })
                  } else if (res.cancel) { //取消
                    wx.reLaunch({
                      url: '/pages/wview/web?status=4&order_no='+that.order_no
                    })
                  }
                }
              });
            }
          }
        })
      }
    })
  },
})